package com.shangcheng.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.mappers.UmsRoleMapper;
import com.shangcheng.dubbo.mappers.UmsRoleMenuRelationMapper;
import com.shangcheng.dubbo.mappers.UmsRoleResourceRelationMapper;
import com.shangcheng.model.domain.UmsRole;
import com.shangcheng.model.domain.UmsRoleMenuRelation;
import com.shangcheng.model.domain.UmsRoleResourceRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.management.relation.Role;
import java.util.Arrays;
import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/14 15:22
 */
@DubboService
public class UmsRoleApiImpl implements UmsRoleApi{
    @Autowired
    private UmsRoleResourceRelationMapper roleResourceRelationMapper;
    @Autowired
    private UmsRoleMenuRelationMapper roleMenuRelationMapper;
    @Autowired
    private UmsRoleMapper umsRoleMapper;
    //获取指定用户的角色,根据roleIds查询所有的角色信息
    @Override
    public List<UmsRole> selectByIds(List<Long> roleIds) {
        LambdaQueryWrapper<UmsRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(UmsRole::getId, roleIds);
        return umsRoleMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 添加角色
     * @param umsRole
     */
    @Override
    public void createRole(UmsRole umsRole) {
        umsRoleMapper.insert(umsRole);
    }

    /**
     * 批量删除角色
     * @param ids
     */
    @Override
    public void deleteRole(Integer[] ids) {
        roleMenuRelationMapper.deleteBatchIds(Arrays.asList(ids.clone()));
        roleResourceRelationMapper.deleteBatchIds(Arrays.asList(ids.clone()));
        umsRoleMapper.deleteBatchIds(Arrays.asList(ids.clone()));
    }

    /**
     * 根据名称查询角色列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<UmsRole> findRoleByName(String keyword, Integer pageNum, Integer pageSize) {
        IPage<UmsRole> page= new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsRole> wrapper= new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(keyword),UmsRole::getName,keyword);
        return umsRoleMapper.selectPage(page,wrapper);
    }

    /**
     * 获取所有角色
     * @return
     */
    @Override
    public List<UmsRole> findAllRole() {
        return umsRoleMapper.selectList(null);
    }

    /**
     * 给角色分配菜单
     * @param menuIds 菜单id
     * @param roleId 角色id
     */
    @Override
    public void allocMenu(Integer[] menuIds, Integer roleId) {
        for (Integer menuId : menuIds) {
            UmsRoleMenuRelation roleMenuRelation = new UmsRoleMenuRelation();
            roleMenuRelation.setMenuId(menuId);
            roleMenuRelation.setRoleId(roleId);
            roleMenuRelationMapper.insert(roleMenuRelation);
        }
    }

    /**
     * 修改角色
     * @param role
     */
    @Override
    public void updateRole(UmsRole role) {
        umsRoleMapper.updateById(role);
    }

    /**
     * 修改角色状态
     * @param role
     */
    @Override
    public void updateRoleStatus(UmsRole role) {
        umsRoleMapper.updateById(role);
    }

    /**
     * 根据roleId查询UmsRoleMenuRelation
     * @param roleId
     * @return
     */
    @Override
    public List<UmsRoleMenuRelation> findMenuIdByRole(Integer roleId) {
        LambdaQueryWrapper<UmsRoleMenuRelation> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleMenuRelation::getRoleId,roleId);
        return roleMenuRelationMapper.selectList(wrapper);
    }

    /**
     * 根据roleId查询UmsRoleResourceRelation
     * @param roleId
     * @return
     */
    @Override
    public List<UmsRoleResourceRelation> findResourceIdByRole(Integer roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleResourceRelation::getRoleId,roleId);
        return roleResourceRelationMapper.selectList(wrapper);
    }

    /**
     * 根据角色全名称查询角色
     * @param umsRole
     * @return
     */
    @Override
    public UmsRole findRoleByAllName(UmsRole umsRole) {
        LambdaQueryWrapper<UmsRole> wrapper= new LambdaQueryWrapper<>();
        wrapper.eq(UmsRole::getName,umsRole.getName());
        return umsRoleMapper.selectOne(wrapper);
    }

}
